$(function() {

    let layer = layui.layer
        // 获取文章id
        // 首先声明一下   随便赋个值也行
        // let a = new URLSearchParams(location.search);
        // 然后通过a.get()的方式输入键 拿到值
        // 拿到前一个页面传过来的id
    let id = new URLSearchParams(location.search).get('id')

    function acquire() {
        $.ajax({
            url: 'http://localhost:8888/api/articles/' + id + '/comments',
            success: function(res) {
                console.log(res);
                if (res.status === 0) {
                    let arr = ['<h4><i class="sprites"></i>评论区</h4>'];
                    res.data.forEach(item => {
                        arr.push(`
                        <div class="kr_comment_card">
                        <div class="img-wrap">
                            <img src="./uploads/avatar_2.jpg" alt="">
                        </div>
                        <div class="info">
                            <p>${item.uname} · <span>2020-08-16</span></p>
                            <p>${item.content}</p>
                        </div>
                        <a href="javascript:;" class="like">${item.count }</a>
                    </div>
                        `)
                    })
                    $('#actives').html(arr.join(''))

                }

            }

        })
    }
    acquire()

    // 发表评论
    $("#form").submit(function(e) {
        e.preventDefault()
        let data = $(this).serialize()
        $.ajax({
            type: "POST",
            url: 'http://localhost:8888/api/articles/' + id + '/comments',
            data,
            success: function(res) {
                // console.log(res);
                if (res.status === 0) {
                    layer.msg("评论成功")
                    $("#form")[0].reset()
                    acquire()
                }
            }
        })
    })

})